package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.ShopVerification;
import com.ruoyi.business.domain.VerificationRecord;
import com.ruoyi.business.domain.Withdrawal;
import com.ruoyi.business.mapper.VerificationRecordMapper;
import com.ruoyi.business.service.IShopVerificationService;
import com.ruoyi.business.service.IVerificationRecordService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 核销记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-11
 */
@Service
public class VerificationRecordServiceImpl implements IVerificationRecordService 
{
    @Resource
    private VerificationRecordMapper verificationRecordMapper;

    @Resource
    private IShopVerificationService shopVerificationService;
    /**
     * 查询核销记录
     * 
     * @param id 核销记录主键
     * @return 核销记录
     */
    @Override
    public VerificationRecord selectVerificationRecordById(Long id)
    {
        return verificationRecordMapper.selectVerificationRecordById(id);
    }

    /**
     * 查询核销记录列表
     * 
     * @param verificationRecord 核销记录
     * @return 核销记录
     */
    @Override
    public List<VerificationRecord> selectVerificationRecordList(VerificationRecord verificationRecord)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles = loginUser.getRoles();
//        商户可查看的
        if (roles.contains("business") && !SecurityUtils.isAdmin(loginUser.getUserid())) {
            String username = loginUser.getSysUser().getUserName();

            ShopVerification shopVerification = new ShopVerification();
            shopVerification.setMobile(username);
//            根据系统用户表的username 查询获取 店铺认证表的 店铺id
            List<ShopVerification> shopVerifications = shopVerificationService.selectShopVerificationList(shopVerification);
            List<Long> mIds = new ArrayList<>();
            shopVerifications.forEach(shopVerification1 -> {
                Long mid = shopVerification1.getMId();
                System.out.println(shopVerification1.getMId());
                mIds.add(mid);
            });
            System.out.println("mIds:" + mIds);
            Long mid = mIds.get(0);
            System.out.println("mid.get[0]:" + mid);
//            List<Orders> ordersList = selectAllByMid(mid);
            verificationRecord.setMId(mid);
            List<VerificationRecord> verList = verificationRecordMapper.selectVerificationRecordList(verificationRecord);
            return verList;
        }
//      管理员可查看的
        return verificationRecordMapper.selectVerificationRecordList(verificationRecord);
    }

    /**
     * 新增核销记录
     * 
     * @param verificationRecord 核销记录
     * @return 结果
     */
    @Override
    public int insertVerificationRecord(VerificationRecord verificationRecord)
    {
        return verificationRecordMapper.insertVerificationRecord(verificationRecord);
    }

    /**
     * 修改核销记录
     * 
     * @param verificationRecord 核销记录
     * @return 结果
     */
    @Override
    public int updateVerificationRecord(VerificationRecord verificationRecord)
    {
        return verificationRecordMapper.updateVerificationRecord(verificationRecord);
    }

    /**
     * 批量删除核销记录
     * 
     * @param ids 需要删除的核销记录主键
     * @return 结果
     */
    @Override
    public int deleteVerificationRecordByIds(Long[] ids)
    {
        return verificationRecordMapper.deleteVerificationRecordByIds(ids);
    }

    /**
     * 删除核销记录信息
     * 
     * @param id 核销记录主键
     * @return 结果
     */
    @Override
    public int deleteVerificationRecordById(Long id)
    {
        return verificationRecordMapper.deleteVerificationRecordById(id);
    }


    // 累计收入
    @Override
    public VerificationRecord selectSum(Double mId) {
        return  verificationRecordMapper.selectSum(mId);
    }



    //当前余额=累计收入-提现金额
    @Autowired
    private RestHighLevelClient elasticsearchClient;
    @Autowired
    private CacheManager cacheManager;

    @Override
    public VerificationRecord selectBalance(Integer mId) {
        VerificationRecord balance = verificationRecordMapper.selectBalance(mId);
        VerificationRecord sum = verificationRecordMapper.selectSum(Double.valueOf(mId));
        balance.setPrice(sum.getPrice()-balance.getPrice());
        return balance;
    }

    @Override
    public List<Withdrawal> selectByMid(Integer mId) {
        return verificationRecordMapper.selectByMid(mId);
    }
}
